package com.unei.groovy.batch

/**
 * Created by sunlei on 2015/1/14.
 */

class GBook{
    String title;
}

def groovyBook=new GBook()
groovyBook.setTitle("haha")
assert groovyBook.getTitle()=="haha";

//GString
def nick="Gina"
def book="Groovy in action"
assert "$nick is $book"=='Gina is Groovy in action'

//regex
assert '12345'=~/\d+/
assert 'xxxxx'=='12345'.replaceAll(/\d/,'x')

//数字也是对象
def x=1
def y=2
assert x+y==3
assert x.plus(y)==3
assert x instanceof Integer

//lists
def roman=['','I','II','III','IV','V','VI','VII']
assert roman[4]=='IV'
roman[8]='VIII'
assert roman.size()==9
println("roman size:"+roman.size())

//maps
println("maps---------------------")
def http=[
        100:'CONTINUE',
        200:'OK',
        400:'BAD REQUEST'
]

println(http[200])
http[500]='INTERNAL SERVER ERROR'
println(http[500])

//ranges
def range=1..10
assert range.contains(5)
assert range.contains(15)==false
assert range.size()==10
assert range.from==1
assert range.to==10
assert range.reverse()==10..1

//closure
println("closure---------------------")
[1,2,3].each {entry -> println entry}

def totalClinks=0
def partyPeople=100
1.upto(partyPeople){guestNumber->
    clinksWithGuest=guestNumber-1
    totalClinks+=clinksWithGuest
}

println("totalClinks:"+(partyPeople*(partyPeople-1))/2)

//control structures
if(false)assert false
if(null)assert false
else assert true

def i=0
while(i<10)i++
assert i==10

def clinks=0
for(remainingGuests in 0..9)
    clinks+=remainingGuests

assert clinks==(10*9)/2

def list=[0,1,2,3,4,5,6,7,8,9]
for(j in list)
    print(j)
println()

list.each {l-> println l}

switch(3){
    case 1:println(1);break
    case 2:println(2);break
    case 3:println(3);break
    default:assert false
}

//dynamic code
def str="println('111')"
evaluate(str)

//String
println("String -----------------------")
def s="""asdfasfasdfasd
asdfasdfasdfasdfasdfasdf
asdfasdfafffffffffffff"""
println(s)

def s1='$s is haha'
println(s1)

//GString

println("GString----------------------------")
me='Tarzan'
you='Jane'
line="me $me - you $you"
println(line)

date =new Date(0)
out ="Year $date.year Month $date.month Day $date.day"
println(out)

//<<
greeting="hello"
println(greeting.getClass())
greeting<<=" Groovy"
println(greeting.getClass())
println(greeting.getMetaClass())

//regex
myFairStringy="The rain in Spain stays mainly in the plain!"
BOUNDS=/\b/
rhyme=/$BOUNDS\w*ain$BOUNDS/
found=''
myFairStringy.eachMatch(rhyme){
    match->found+=match[0]+' '
}

println(found)

found=''
(myFairStringy=~rhyme).each {match->found+=match+' '}
println(found)

cloze=myFairStringy.replaceAll(rhyme){it-'ain'+"___"}
println(cloze)

for(element in 0..<10){
    print(element+" ")
}

//range
class Weekday implements Comparable{
    static final DAYS=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
    private int index=0
    Weekday(String day){
        index=DAYS.indexOf(day)
    }

    Weekday next(){
        return new Weekday(DAYS[(index+1)%DAYS.size()])
    }

    Weekday previous(){
        return new Weekday(DAYS[index-1])
    }

    int compareTo(Object other){
        return this.index<=>other.index;
    }

    String toString(){
        return DAYS[index]
    }
}

def mon=new Weekday("Mon")
def fri=new Weekday("Fri")

def worklog=""

for(day in mon..fri){
    worklog+=day.toString()+ ' '
}

println(worklog)
